package ac.svn.accesscontrol.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class MainWindow extends JFrame implements IUIConstants 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private XMenuBar menuBar;
	private JToolBar statusBar;
	private JLabel statusLabel;
	private JLabel errorLabel;
	private JToolBar toolBar;
	private JPanel basePanel;
	private JList locationListView;
	private DefaultListModel locationListModel;
	private JList accessListView;
	private DefaultListModel accessListModel;
	private JScrollPane treeScrollPane;
	private JTree svnTree;
	private DefaultMutableTreeNode rootNode;
	private JTextField userSearchTxt;
	private JTextField locationSearchTxt;
	
	private JTable tableView;

	public MainWindow()
	{
		super("Smart CM");
		initComponents();
		registerComponents();
	}

	private void registerComponents() {
		UIComponentLibrary.addComponent(C_MAIN_WINDOW, this);
		UIComponentLibrary.addComponent(C_MENU_BAR, menuBar);
		UIComponentLibrary.addComponent(C_STATUS_BAR, statusBar);
		UIComponentLibrary.addComponent(C_STATUS_LABEL, statusLabel);
		UIComponentLibrary.addComponent(C_ERROR_LABEL,errorLabel);
		UIComponentLibrary.addComponent(C_TOOL_BAR, toolBar);
		UIComponentLibrary.addComponent(C_LOCATION_MODEL,locationListModel);
		UIComponentLibrary.addComponent(C_LOCATION_LIST,locationListView);
		UIComponentLibrary.addComponent(C_ACCESS_MODEL,accessListModel);
		UIComponentLibrary.addComponent(C_ACCESS_LIST,accessListView);
		UIComponentLibrary.addComponent(C_USERS_TREE,svnTree);
		UIComponentLibrary.addComponent(C_ROOT_NODE,rootNode);
		UIComponentLibrary.addComponent(C_TEXT_S_LOCATION,locationSearchTxt);
		UIComponentLibrary.addComponent(C_TEXT_S_USER,userSearchTxt);
	}

	private void initComponents() {
		setLayout(new BorderLayout(5,5));
		setSize(600,400);
		setLocation();

		menuBar = new XMenuBar();
		setJMenuBar(menuBar);

		statusLabel = new JLabel("Welcome to Smart CM");
		errorLabel = new JLabel("Issues: 0");
		errorLabel.setForeground(Color.RED);
		statusBar = new JToolBar();
		statusBar.add(statusLabel);
		statusBar.addSeparator();
		statusBar.add(errorLabel);
		statusBar.addSeparator();

		getContentPane().add(statusBar,BorderLayout.SOUTH);

		toolBar = new JToolBar();
		JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEADING),true);
		pnl.setSize(500,60);
		userSearchTxt = new JTextField(15);
		locationSearchTxt = new JTextField(15);
		
		pnl.add(new JLabel("Search User/Group[F3]:"));
		pnl.add(userSearchTxt);
		pnl.add(new JLabel("Search Location[F4]:"));
		pnl.add(locationSearchTxt);
		
		toolBar.add(pnl);
		toolBar.addSeparator();
		getContentPane().add(toolBar,BorderLayout.NORTH);
		basePanel = new JPanel(new BorderLayout(5, 5),true);

		rootNode = new DefaultMutableTreeNode("Users");
		svnTree = new JTree(rootNode);
		svnTree.setCellRenderer(new UserTreeCellRenderer());
		svnTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
		ToolTipManager.sharedInstance().registerComponent(svnTree);
		treeScrollPane = new JScrollPane(svnTree);
		treeScrollPane.setPreferredSize(new Dimension(200, 700));
		treeScrollPane.setBorder(new TitledBorder("Groups"));
		treeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		basePanel.add(treeScrollPane,BorderLayout.WEST);
		//svnTree.setScrollsOnExpand(true);
		
		JScrollPane scrollPane;
		
		JPanel bodyPanel = new JPanel((new BorderLayout(5, 5)),true);
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPane.setBorder(new TitledBorder("SVN"));
		
		locationListModel = new DefaultListModel();
		locationListView = new JList(locationListModel);
		locationListView.setCellRenderer(new SimpleIconRenderer("images/url.png"));
		scrollPane = new JScrollPane(locationListView);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBorder(new TitledBorder("Locations"));
		scrollPane.setPreferredSize(new Dimension(500, 300));
		//bodyPanel.add(scrollPane,BorderLayout.CENTER);
		splitPane.setTopComponent(scrollPane);
		
		accessListModel = new DefaultListModel();
		accessListView  = new JList(accessListModel);
		accessListView.setCellRenderer(new AccessListCellRenderer());
		scrollPane = new JScrollPane(accessListView);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBorder(new TitledBorder("Rights"));
		scrollPane.setPreferredSize(new Dimension(500, 200));
		//bodyPanel.add(scrollPane,BorderLayout.SOUTH);
		splitPane.setBottomComponent(scrollPane);
		
		splitPane.addComponentListener(new ComponentAdapter() {
		    @Override
			public void componentResized(ComponentEvent arg0) {
				JSplitPane s = (JSplitPane) arg0.getSource();
				s.setDividerLocation(.75);
			}
		});
		
		bodyPanel.add(splitPane,BorderLayout.CENTER);
		
		basePanel.add(bodyPanel,BorderLayout.CENTER);
		

		getContentPane().add(basePanel,BorderLayout.CENTER);
	}

	public void setLocation() {
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(size.getWidth()/2 - getWidth()/2),(int)(size.getHeight()/2-getHeight()/2));
	}


	public static void main(String args[]) throws Exception
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		MainWindow mainWindow = new MainWindow();
		mainWindow.setVisible(true);
	}


}
